package com.aaa.mrcai.service.impl;
import com.aaa.mrcai.mapper.GoodsDepositMapper;
import com.aaa.mrcai.pojo.entity.GoodsDeposit;
import com.aaa.mrcai.pojo.vo.GoodsDepositVo;
import com.aaa.mrcai.query.GoodsDepositQuery;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.GoodsDepositService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Demo class
 *
 * @Author: Wang
 * @Date: Created in 0:07 2021/12/7
 */
@Service
public class GoodsDepositServiceImpl implements GoodsDepositService {
    @Autowired
    private GoodsDepositMapper goodsDepositMapper;

    @Autowired
    private HttpSession session;


    @Override
    public AjaxResult list(GoodsDepositQuery query) {
        List<GoodsDeposit> goodsDepositList = goodsDepositMapper.queryGoodsDeposit(query);
        Long totalCount = goodsDepositMapper.queryGoodsDepositTotalCount(query);

        Map<Object, Object> map = new HashMap<>();
        map.put("currentPageData",goodsDepositList);
        map.put("currentDataTotal", totalCount);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult addGoodsDeposit(GoodsDepositVo goodsDepositVo) {
        long count = goodsDepositMapper.isGoodsDepositExists(goodsDepositVo.getState());
        if (count > 0) {
            return AjaxResult.defeated("对不起，此储存方式已存在");
        }
        int i = goodsDepositMapper.addGoodsDeposit(goodsDepositVo);

        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }

    @Override
    public AjaxResult getGoodsDeposit(int id) {
        GoodsDepositVo r = goodsDepositMapper.getGoodsDeposit(id);
        session.setAttribute("currentUpdateGoodsDeposit",r);
        return AjaxResult.success(r);
    }

    @Override
    public AjaxResult updateGoodsDeposit(GoodsDepositVo goodsDepositVo) {
        GoodsDepositVo currentUpdateGoodsDeposit= (GoodsDepositVo) session.getAttribute("currentUpdateGoodsDeposit");

        if (!goodsDepositVo.getId().equals(currentUpdateGoodsDeposit.getId())){
            return AjaxResult.defeated("不可以乱改哦");

        }
        long count = goodsDepositMapper.isUpdateGoodsDepositExists(goodsDepositVo);

        if (count > 0) {
            return AjaxResult.defeated("已存在该商品品牌");
        }

        int i = goodsDepositMapper.updateGoodsDeposit(goodsDepositVo);

        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }

    @Override
    public AjaxResult deleteGoodsDeposit(int id) {
        int  r = goodsDepositMapper.deleteGoodsDeposit(id);
        session.setAttribute("currentUpdateGoodsDeposit",r);
        return AjaxResult.success(r);
    }
}
